﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Greve
{
    public partial class Judge_ui : Form
    {

        public delegate void TickHandler(Judge_ui j, EventArgs e);

        public event TickHandler Tick;

        public EventArgs ee = null;



		NetworkClient client;
        public Judge_ui()
        {
            InitializeComponent();
			client = new NetworkClient();

        }

        private void Judge_ui_Load(object sender, EventArgs e)
        {

        }

		private void textBox1_TextChanged(object sender, EventArgs e)
		{

		}

		private void button8_Click(object sender, EventArgs e)
		{
			if (checkBox1.Checked == true)
			{
				Button b = (Button)sender;
				textBox_penalty.Text = textBox_penalty.Text + b.Text;
			}
			else
			{
				Button b = (Button)sender;
				points_text.Text = points_text.Text + b.Text;
			}
		}

		private void button11_Click(object sender, EventArgs e)
		{
			if(checkBox1.Checked == true)
			{
				textBox_penalty.ResetText();
			}
			else
				points_text.ResetText();
		}

		private void points_text_TextChanged(object sender, EventArgs e)
		{
			if(points_text.Text.StartsWith(","))
			{
				points_text.Text = "0,";
			}
			if(points_text.Text.Length > 3)
			{
				points_text.Text = points_text.Text.Remove(0, 3);
			}
			double pts = 0;
			if(points_text.Text.Length != 0)
			{
				pts = Double.Parse(points_text.Text.ToString());
				if (pts > 10)
				{
					points_text.Text = "10";
				}
			}
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			textBox_penalty.Enabled = checkBox1.Checked;
			points_text.Enabled = !checkBox1.Checked;
			
		}

		private void textBox_penalty_TextChanged(object sender, EventArgs e)
		{
			if(textBox_penalty.Text.Length > 1 || textBox_penalty.Text.Contains(","))
			{
				textBox_penalty.Text = textBox_penalty.Text.Remove(0,1);
			}
			double pts = 0;
			if (textBox_penalty.Text.Length > 0)
			{
				pts = Double.Parse(textBox_penalty.Text.ToString());
				if (pts > 4)
				{
					textBox_penalty.Text = "4";
				}
			}
		}

		public void button1_Click(object sender, EventArgs e)
		{
            if (Tick != null)
            {
                Tick(this, ee);
            }
		}

        public String getValues()
        {
            if(points_text.Text.Equals(""))
                points_text.Text = "-1";
            return points_text.Text + " [" + checkBox1.Checked + "] " + textBox_penalty.Text;
        }

        public void setNewJumper(String str)
        {
            Invoke((MethodInvoker)delegate
            {
                if (!("Current Jumper "+str).Equals(label1.Text))
                {
                    label1.Text = "Current Jumper "+str;
                    points_text.Text = "";
                    textBox_penalty.Text = "";
                }
            });
        }
    }
}